﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Notas

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property
    Private log__1 As New Class_RC_Controller.rc_Mantenimiento()

#Region "Notas"

    Public Function get_Rango_Pedido(conn As String) As String
        Try

            Dim rango As New Class_RC_Data.Class_RC.RANGODOCUMENTOS(conn)
            Dim numero As String = ""
            rango.Where.TipoDocumento.Value = "PEDIDO"
            rango.Where.Habilitado.Value = True

            If rango.Query.Load Then
                If rango.DefaultView.Table.Rows.Count > 0 Then

                    numero = rango.Prefijo.ToString() + rango.RangoActual.ToString().PadLeft(7, "0")
                    rango.RangoActual += 1
                    rango.Save()

                    Return numero
                Else
                    Return ""
                End If

            Else
                Return ""

            End If

        Catch ex As Exception
            MessageError = ex
            Return ""
        End Try

    End Function

    Public Function Insert_Notas(conn As String, nroNota As String, codCliente As Integer, fecha As DateTime, codEstado As Integer, codContacto As Integer,
                                 total As Decimal, codTipoNota As Integer, direccion As String, fechaVto As String, codOrdenCompra As Integer,
                                 obs As String, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim notas As New Class_RC_Data.Class_RC.NOTAS(conn)

            notas.AddNew()

            If nroNota = "" Then
                nroNota = get_Rango_Pedido(conn)
            End If

            notas.NroNota = nroNota
            notas.CodCliente = codCliente
            notas.Fecha = fecha
            notas.CodEstadoNota = codEstado
            notas.CodContacto = codContacto
            notas.Total = total
            notas.CodTipoNota = codTipoNota
            notas.DireccionEntrega = direccion

            If fechaVto <> "" Then
                notas.FechaVencimiento = DateTime.Parse(fechaVto)
            End If

            If codOrdenCompra > 0 Then
                notas.CodOrdenCompra = codOrdenCompra
            End If

            notas.Observaciones = obs
            notas.CodUsuario = codUsuario
            notas.FechaActualizacion = DateTime.Now
            notas.Save()
            log__1.Insert_Log(conn, "Inserto registro, codigo: " & notas.CodNota, codUsuario, 21, notas.DefaultView.Table)
            tx.CommitTransaction()

            Return notas.CodNota

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Notas(conn As String, codNota As Integer,
                                     nroNota As String, codCliente As Integer, fecha As DateTime, codEstado As Integer, codContacto As Integer,
                                 total As Decimal, codTipoNota As Integer, direccion As String, fechaVto As String, codOrdenCompra As Integer,
                                 obs As String, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim notas As New Class_RC_Data.Class_RC.NOTAS(conn)
            notas.Where.CodNota.Value = codNota

            If notas.Query.Load Then
                log__1.Insert_Log(conn, "Actualiza registro anterior, codigo:" & notas.CodNota, codUsuario, 22, notas.DefaultView.Table)
                notas.NroNota = nroNota
                notas.CodCliente = codCliente
                notas.Fecha = fecha
                notas.CodEstadoNota = codEstado
                notas.CodContacto = codContacto
                notas.Total = total
                notas.CodTipoNota = codTipoNota
                notas.DireccionEntrega = direccion

                If fechaVto <> "" Then
                    notas.FechaVencimiento = DateTime.Parse(fechaVto)
                End If

                If codOrdenCompra > 0 Then
                    notas.CodOrdenCompra = codOrdenCompra
                End If

                notas.Observaciones = obs

                notas.CodUsuario = codUsuario
                notas.FechaActualizacion = DateTime.Now
                notas.Save()

            End If
            log__1.Insert_Log(conn, "Actualiza registro editado, codigo:" & notas.CodNota, codUsuario, 22, notas.DefaultView.Table)
            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Update_Notas_Estado(conn As String, codNota As Integer,
                                     codEstado As Integer, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim notas As New Class_RC_Data.Class_RC.NOTAS(conn)
            notas.Where.CodNota.Value = codNota

            If notas.Query.Load Then

                notas.CodEstadoNota = codEstado
                notas.CodUsuario = codUsuario
                notas.FechaActualizacion = DateTime.Now
                notas.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Notas(ByVal conn As String, ByVal codNota As Integer, ByVal codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim nota As New Class_RC_Data.Class_RC.NOTAS(conn)
            nota.Where.CodNota.Value = codNota

            If nota.Query.Load Then

                nota.MarkAsDeleted()
                nota.Save()

            End If
            log__1.Insert_Log(conn, "Inserto registro, codigo: " & nota.CodNota, codUsuario, 21, nota.DefaultView.Table)
            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Notas_All(conn As String, codNota As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, codTipoNota As Integer, cliente As String,
                                   codEstado As Integer, nroNota As String, ruc As String,
                                   contacto As String, soloPendientes As Boolean, codEstadoFinalizado As Integer) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotas(conn)

            If codNota > 0 Then
                vNotas.Where.CodNota.Value = codNota
            End If

            'If codCliente > 0 Then
            '    vNotas.Where.CodCliente.Value = codCliente
            'End If

            If contacto <> "" Then
                vNotas.Where.Contacto.Value = "%" & contacto & "%"
                vNotas.Where.Contacto.Operator = WhereParameter.Operand.Like
            End If

            If nroNota <> "" And IsNumeric(nroNota) Then
                vNotas.Where.NroNota.Value = nroNota
            Else
                vNotas.Where.Fecha.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vNotas.Where.Fecha.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vNotas.Where.Fecha.Operator = WhereParameter.Operand.Between
            End If

            If codTipoNota > 0 Then
                vNotas.Where.CodTipoNota.Value = codTipoNota
            End If

            If Not soloPendientes Then

                If codEstado > 0 Then
                    vNotas.Where.CodEstadoNota.Value = codEstado
                End If

                If cliente <> "" Then
                    vNotas.Where.Nombre.Value = "%" + cliente + "%"
                    vNotas.Where.Nombre.Operator = WhereParameter.Operand.Like
                End If

            Else

                If codEstadoFinalizado > 0 Then
                    vNotas.Where.CodEstadoNota.Value = codEstadoFinalizado
                    vNotas.Where.CodEstadoNota.Operator = WhereParameter.Operand.NotEqual
                End If

            End If

            If ruc <> "" Then
                vNotas.Where.Ruc.Value = "%" + ruc + "%"
                vNotas.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            vNotas.Query.AddOrderBy("fecha", WhereParameter.Dir.DESC)

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_Impresion(ByVal conn As String, ByVal codNota As Integer, ByVal fechaInicio As DateTime,
                                   ByVal fechaFin As DateTime, ByVal codTipoNota As Integer, ByVal cliente As String,
                                   ByVal codEstado As Integer, ByVal nroNota As String, ByVal ruc As String,
                                   ByVal contacto As String) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotasReporte(conn)

            If codNota > 0 Then
                vNotas.Where.CodNota.Value = codNota
            End If

            'If codCliente > 0 Then
            '    vNotas.Where.CodCliente.Value = codCliente
            'End If

            If contacto <> "" Then
                vNotas.Where.Contacto.Value = "%" & contacto & "%"
                vNotas.Where.Contacto.Operator = WhereParameter.Operand.Like
            End If

            If nroNota <> "" And IsNumeric(nroNota) Then
                vNotas.Where.NroNota.Value = nroNota
            Else
                vNotas.Where.Fecha.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vNotas.Where.Fecha.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vNotas.Where.Fecha.Operator = WhereParameter.Operand.Between
            End If

            If codTipoNota > 0 Then
                vNotas.Where.CodTipoNota.Value = codTipoNota
            End If

            If codEstado > 0 Then
                vNotas.Where.CodEstadoNota.Value = codEstado
            End If

            If cliente <> "" Then
                vNotas.Where.Nombre.Value = "%" + cliente + "%"
                vNotas.Where.Nombre.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vNotas.Where.Ruc.Value = "%" + ruc + "%"
                vNotas.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            vNotas.Query.AddOrderBy("fecha", WhereParameter.Dir.DESC)

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_Imprimir(ByVal conn As String, ByVal codNota As Integer) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotasReporte(conn)

            If codNota > 0 Then
                vNotas.Where.CodNota.Value = codNota
            End If
            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_ByID(conn As String, codNota As Integer, estado As Integer) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotas(conn)

            If codNota > 0 Then
                vNotas.Where.CodNota.Value = codNota
            End If

            If estado > 0 Then
                vNotas.Where.CodEstadoNota.Value = estado
            End If

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_ByID(conn As String, codNota As Integer) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotas(conn)

            If codNota > 0 Then
                vNotas.Where.CodNota.Value = codNota
            End If

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_ByNRO(conn As String, nroNota As String, codEstadoFinalizado As Integer, paraVenta As Boolean) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotas(conn)
            vNotas.Where.NroNota.Value = nroNota

            If codEstadoFinalizado > 0 Then
                vNotas.Where.CodEstadoNota.Value = codEstadoFinalizado
            End If

            If paraVenta Then
                vNotas.Where.CodVenta.Value = 0
                vNotas.Where.CodVenta.Operator = WhereParameter.Operand.IsNull
            End If

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_Detalle_ByID(conn As String, codNotaDetalle As Integer) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaNotasDetalle(conn)

            If codNotaDetalle > 0 Then
                vNotas.Where.CodNotaDetalle.Value = codNotaDetalle
            End If

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Notas_Detalle(conn As String, codNota As Integer, codProducto As Integer, cantidad As Decimal,
                                          precio As Decimal, codActividad As Integer, estadoAct As String,
                                          codResponsable As Integer, codServicio As Integer, iva As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLENOTAS(conn)

            detalle.AddNew()
            detalle.CodNota = codNota

            If codProducto > 0 Then
                detalle.CodProducto = codProducto
            Else
                detalle.SetColumnNull("CodProducto")
            End If

            detalle.Cantidad = cantidad
            detalle.Precio = precio
            detalle.CodActividad = codActividad
            detalle.EstadoActividad = estadoAct
            detalle.CodResponsable = codResponsable
            detalle.Iva = iva

            If codServicio > 0 Then
                detalle.CodServicio = codServicio
            Else
                detalle.SetColumnNull("CodServicio")
            End If

            detalle.Save()

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Notas_Detalle(conn As String, codNotaDetalle As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLENOTAS(conn)

            detalle.Where.CodNotaDetalle.Value = codNotaDetalle

            If detalle.Query.Load Then
                detalle.MarkAsDeleted()
                detalle.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Notas_Detalles_All(conn As String, codNota As Integer, soloProductos As Boolean, soloFinalizados As Boolean,
                                           paraDeposito As Boolean) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaNotasDetalle(conn)

            If codNota > 0 Then
                vDetalle.Where.CodNota.Value = codNota
            End If

            If soloProductos Then
                vDetalle.Where.CodProducto.Value = 0
                vDetalle.Where.CodProducto.Operator = WhereParameter.Operand.IsNotNull
            End If

            If soloFinalizados Then
                vDetalle.Where.EstadoActividad.Value = "FINALIZADO"
            End If

            If paraDeposito Then
                vDetalle.Where.CodSeccion.Value = 0
                vDetalle.Where.CodSeccion.Operator = WhereParameter.Operand.IsNull
            End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Notas_Detalles_Materiales_All(conn As String, codNota As Integer, soloSaldo As Boolean) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaNotasDetalleMaterial(conn)

            If codNota > 0 Then
                vDetalle.Where.CodNota.Value = codNota
            End If

            If soloSaldo Then
                vDetalle.Where.Saldo.Value = 0
                vDetalle.Where.Saldo.Operator = WhereParameter.Operand.GreaterThan
            End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_tipo_notas(conn As String) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.TIPONOTA(conn)

            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_estado_notas(conn As String, codEstadoFinalizado As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.ESTADONOTAS(conn)
            vTipo.Where.CodTipoNota.Value = 1

            If codEstadoFinalizado > 0 Then
                vTipo.Where.CodEstadoNota.Value = codEstadoFinalizado
                vTipo.Where.CodEstadoNota.Operator = WhereParameter.Operand.NotEqual
            End If

            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    'Public Function get_estado_actividad(conn As String) As DataTable
    '    Try

    '        Dim vTipo As New Class_RC_Data.Class_RC.ESTADONOTAS(conn)
    '        vTipo.Where.CodTipoNota.Value = 1
    '        If vTipo.Query.Load Then
    '            If vTipo.DefaultView.Table.Rows.Count > 0 Then
    '                Return vTipo.DefaultView.Table
    '            Else
    '                Return Nothing
    '            End If

    '        Else
    '            Return Nothing

    '        End If

    '    Catch ex As Exception
    '        MessageError = ex
    '        Return Nothing
    '    End Try

    'End Function

    Public Function get_actividad_sector_notas(conn As String, codSector As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.ACTIVIDADES(conn)
            vTipo.Where.CodSector.Value = codSector
            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_sector_area_notas(conn As String, codArea As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.SECTORES(conn)
            vTipo.Where.CodArea.Value = codArea

            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_areas_notas(conn As String) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.AREAS(conn)

            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_responsable_actividad_notas(conn As String, codActividad As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.VistaProductosActividades(conn)
            vTipo.Where.CodActividad.Value = codActividad

            If vTipo.Query.Load Then

                If vTipo.DefaultView.Table.Rows.Count > 0 Then

                    Dim vTipoEmp As New Class_RC_Data.Class_RC.VistaEmpleadoCombo(conn)
                    vTipoEmp.Where.CodTipoEmp.Value = vTipo.CodTipoEmp

                    If vTipoEmp.Query.Load Then

                        If vTipoEmp.DefaultView.Table.Rows.Count > 0 Then
                            Return vTipoEmp.DefaultView.Table
                        Else
                            Return Nothing

                        End If

                    Else
                        Return Nothing

                    End If

                Else
                    Return Nothing

                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_actividad_producto(conn As String, codProducto As Integer, codNotaDetalle As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.USUARIOS(conn)
            Dim ds As DataSet

            ds = vTipo.proc_actividades_productos_mov(conn, codProducto, codNotaDetalle)

            If Not ds Is Nothing Then
                If ds.Tables.Count > 0 Then
                    If ds.Tables(0).Rows.Count > 0 Then
                        Return ds.Tables(0)
                    Else
                        Return Nothing
                    End If
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_sector_actividad(conn As String, codActividad As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.ACTIVIDADES(conn)
            vTipo.Where.CodActividad.Value = codActividad

            If vTipo.Query.Load Then

                If vTipo.DefaultView.Table.Rows.Count > 0 Then

                    Dim vTipoSector As New Class_RC_Data.Class_RC.SECTORES(conn)
                    vTipoSector.Where.CodSector.Value = vTipo.CodSector
                    If vTipoSector.Query.Load Then
                        Return vTipoSector.DefaultView.Table

                    Else
                        Return Nothing

                    End If

                Else
                    Return Nothing

                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing

        End Try

    End Function

    Public Function get_area_sector(conn As String, codSector As Integer) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.SECTORES(conn)

            vTipo.Where.CodSector.Value = codSector

            If vTipo.Query.Load Then

                If vTipo.DefaultView.Table.Rows.Count > 0 Then

                    Dim vTipoArea As New Class_RC_Data.Class_RC.AREAS(conn)
                    vTipoArea.Where.CodArea.Value = vTipo.CodArea
                    If vTipoArea.Query.Load Then
                        Return vTipoArea.DefaultView.Table

                    Else
                        Return Nothing

                    End If

                Else
                    Return Nothing

                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "MovNotas"

    Public Function Control_Estado_Finalizado_Servicio(conn As String, codNota As Integer, codEstadoCierre As Integer, codUsuario As Integer) As Boolean
        Try

            Dim NotasDetalleControl As New Class_RC_Data.Class_RC.VistaNotasDetalle(conn)

            NotasDetalleControl.Where.CodNota.Value = codNota
            NotasDetalleControl.Where.EstadoActividad.Value = "FINALIZADO"
            NotasDetalleControl.Where.EstadoActividad.Operator = WhereParameter.Operand.NotLike
            NotasDetalleControl.Where.CodProducto.Value = 0
            NotasDetalleControl.Where.CodProducto.Operator = WhereParameter.Operand.IsNull

            If Not NotasDetalleControl.Query.Load Then

                If NotasDetalleControl.DefaultView.Table.Rows.Count = 0 Then
                    Dim Notas As New Class_RC_Data.Class_RC.NOTAS(conn)
                    Notas.Where.CodNota.Value = codNota
                    If Notas.Query.Load Then
                        Notas.CodEstadoNota = codEstadoCierre
                        Notas.CodUsuario = codUsuario
                        Notas.FechaActualizacion = DateTime.Now
                        Notas.Save()
                    End If
                End If

            End If

            Return True

        Catch ex As Exception
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Control_Estado_Finalizado(conn As String, codNota As Integer, codEstadoCierre As Integer, codUsuario As Integer) As Boolean
        Try

            Dim NotasDetalleControl As New Class_RC_Data.Class_RC.VistaDetalleNotaActividadEstado(conn)
            NotasDetalleControl.Where.CodNota.Value = codNota
            NotasDetalleControl.Where.Estado.Value = "FINALIZADO"
            NotasDetalleControl.Where.Estado.Operator = WhereParameter.Operand.NotLike
            
            If Not NotasDetalleControl.Query.Load Then

                If NotasDetalleControl.DefaultView.Table.Rows.Count = 0 Then
                    Dim Notas As New Class_RC_Data.Class_RC.NOTAS(conn)
                    Notas.Where.CodNota.Value = codNota
                    If Notas.Query.Load Then
                        Notas.CodEstadoNota = codEstadoCierre
                        Notas.CodUsuario = codUsuario
                        Notas.FechaActualizacion = DateTime.Now
                        Notas.Save()
                    End If
                End If

            Else

                Return False

            End If

            Return True

        Catch ex As Exception
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Insert_Mov_Notas(conn As String, codActividad As Integer, codEmpleado As Integer,
                                     codUsuario As Integer, estado As String, fechaInicio As DateTime, fechaFin As String, obs As String,
                                     codNotaDetalle As Integer, codNota As Integer, codEstadoProduccion As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()
            Dim nota As New Class_RC_Data.Class_RC.NOTAS(conn)
            nota.Where.CodNota.Value = codNota
            If nota.Query.Load Then
                nota.CodEstadoNota = codEstadoProduccion
                nota.CodUsuario = codUsuario
                nota.FechaActualizacion = DateTime.Now
                nota.Save()
            End If
            Dim mov As New Class_RC_Data.Class_RC.MOV_DETA_PEDIDO(conn)

            mov.AddNew()
            mov.CodNotaDetalle = codNotaDetalle
            mov.CodActividad = codActividad
            mov.CodEmpleado = codEmpleado
            mov.CodUsuario = codUsuario
            mov.Estado = estado
            mov.FechaInicio = fechaInicio

            If fechaFin <> "" And estado = "FINALIZADO" Then
                mov.FechaFinal = DateTime.Parse(fechaFin)
            Else
                mov.SetColumnNull("FechaFinal")
            End If

            mov.Observacion = obs
            mov.FechaActualizacion = DateTime.Now
            mov.Save()

            Dim NotasDetalle As New Class_RC_Data.Class_RC.DETALLENOTAS(conn)
            NotasDetalle.Where.CodNotaDetalle.Value = codNotaDetalle
            If NotasDetalle.Query.Load Then
                NotasDetalle.CodActividad = codActividad
                NotasDetalle.EstadoActividad = estado
                NotasDetalle.CodResponsable = codEmpleado
                NotasDetalle.Save()
            End If

            tx.CommitTransaction()

            Return mov.CodMovDetPedido

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Mov_Notas(conn As String, codActividad As Integer, codEmpleado As Integer, codUsuario As Integer,
                                     estado As String, fechaFinal As String, obs As String, codNotaDetalle As Integer,
                                     codNota As Integer, codEstadoCierre As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim notas As New Class_RC_Data.Class_RC.MOV_DETA_PEDIDO(conn)
            notas.Where.CodActividad.Value = codActividad
            notas.Where.CodNotaDetalle.Value = codNotaDetalle

            If notas.Query.Load Then

                notas.CodEmpleado = codEmpleado
                notas.CodUsuario = codUsuario
                notas.Estado = estado

                If fechaFinal <> "" Then
                    notas.FechaFinal = DateTime.Parse(fechaFinal)
                Else
                    notas.SetColumnNull("FechaFinal")
                End If

                notas.Observacion = obs
                notas.FechaActualizacion = DateTime.Now
                notas.Save()

                Dim notaDetalle As New Class_RC_Data.Class_RC.DETALLENOTAS(conn)
                notaDetalle.Where.CodNotaDetalle.Value = codNotaDetalle
                If notaDetalle.Query.Load Then
                    notaDetalle.EstadoActividad = estado
                    notaDetalle.CodResponsable = codEmpleado
                    notaDetalle.Save()
                End If

                Dim NotasDetalleControl As New Class_RC_Data.Class_RC.VistaDetalleNotaActividadEstado(conn)
                NotasDetalleControl.Where.CodNota.Value = codNota
                NotasDetalleControl.Where.Estado.Value = "FINALIZADO"
                NotasDetalleControl.Where.Estado.Operator = WhereParameter.Operand.NotEqual
                If Not NotasDetalleControl.Query.Load Then

                    Dim notaPedido As New Class_RC_Data.Class_RC.NOTAS(conn)
                    notaPedido.Where.CodNota.Value = codNota
                    If notaPedido.Query.Load Then
                        notaPedido.CodEstadoNota = codEstadoCierre
                        notaPedido.Save()
                    End If

                End If

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Mov_Notas(conn As String, codMovNotaDetalle As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.MOV_DETA_PEDIDO(conn)

            detalle.Where.CodMovDetPedido.Value = codMovNotaDetalle

            If detalle.Query.Load Then
                detalle.MarkAsDeleted()
                detalle.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Mov_Notas_ByID(conn As String, codNotaDetalle As Integer, soloPendientes As Boolean) As DataTable
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaMovDetProductos(conn)
            vNotas.Where.CodNotaDetalle.Value = codNotaDetalle

            If soloPendientes Then
                vNotas.Where.FechaFinal.Value = 0
                vNotas.Where.FechaFinal.Operator = WhereParameter.Operand.IsNull
            End If

            vNotas.Query.AddOrderBy("CodMovDetPedido", WhereParameter.Dir.DESC)

            If vNotas.Query.Load Then
                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return vNotas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Mov_Notas_Act_Finalizada(conn As String, codNotaDetalle As Integer, codActividad As Integer) As Boolean
        Try

            Dim vNotas As New Class_RC_Data.Class_RC.VistaMovDetProductos(conn)
            vNotas.Where.CodNotaDetalle.Value = codNotaDetalle
            vNotas.Where.CodActividad.Value = codActividad
            vNotas.Where.Estado.Value = "FINALIZADO"

            If vNotas.Query.Load Then

                If vNotas.DefaultView.Table.Rows.Count > 0 Then
                    Return True
                Else
                    Return False
                End If

            Else

                Return False

            End If

        Catch ex As Exception
            MessageError = ex
            Return False
        End Try

    End Function

#End Region

End Class
